package pl.donajski.worldBrowser.services.dao;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import pl.donajski.worldBrowser.entities.Country;

@Repository
@Transactional
public class HibernateDao implements IWorldDAO, Serializable {

	private static final long serialVersionUID = 1L;
	
	private SessionFactory sessionFactory;
	
	@Resource(name="sessionFactory")
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	@Transactional(readOnly=true)
	public List getAllCountries() {
		System.out.println("Ijected sessionFactory: + " +  sessionFactory);
		return sessionFactory.getCurrentSession().createQuery("from Country c order by c.continent, c.name").list();
	}
	
	@Transactional(readOnly=true)
	public Collection getAllCities() {
		// TODO Auto-generated method stub
		return null;
	}

	@Transactional(readOnly=true)
	public Collection getAllLanguages() {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Transactional(readOnly=true)
	public Country getCountryByCode(String code) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Transactional(readOnly=true)
	public List getCitiesByCountryCode(String code) {
		return sessionFactory.getCurrentSession().
				createQuery("from City c where countryCode=? order by c.population").
				setString(0, code).
				list();
	}

}
